﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;

namespace WuxiWTO.Web
{
    public partial class SearchResult : System.Web.UI.Page
    {
        WTODataContext ctx = new WTODataContext();
        PagedDataSource psd = new PagedDataSource();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindParentCategory();
                InitGrid();
            }
        }

        private void BindParentCategory()
        {
            this.ddlParent.Items.Clear();
            var parent = ctx.categories.Where(p => p.art_parent_kind == 0).ToList();
            foreach (category cat in parent)
            {
                this.ddlParent.Items.Add(new ListItem(cat.art_kind_name, cat.art_kind_id.ToString()));
            }
            this.ddlParent.Items.Insert(0, new ListItem("--请选择--", ""));
        }

        private void InitGrid()
        {
            if (!IsPostBack)
            {
                string pid = Request.QueryString["pid"];
                string keywords = Request.QueryString["keywords"];
                if (!string.IsNullOrEmpty(pid))
                {
                    ddlParent.Items.FindByValue(pid).Selected = true;
                }
                if (!string.IsNullOrEmpty(keywords))
                {
                    this.txtKeyWords.Text = keywords;
                }
            }

            if (string.IsNullOrEmpty(ddlParent.SelectedItem.Value) && string.IsNullOrEmpty(txtKeyWords.Text.Trim()))
            {
                return;
            }
            else
            {
                var list = from c in ctx.articles orderby c.update_date descending select c;
                var query = list.AsQueryable();

                if (this.txtKeyWords.Text.Trim() != "")
                {
                    query = query.Where(p => p.article_title.ToLower().Contains(this.txtKeyWords.Text.Trim().ToLower()));
                }
                if (this.ddlParent.SelectedItem.Value != "")
                {
                    int parent = int.Parse(this.ddlParent.SelectedItem.Value);
                    query = query.Where(p => p.parent_category_id == parent);
                }
                List<article> articles = query.ToList();

                AspNetPager1.RecordCount = articles.Count;

                psd.DataSource = articles;
                psd.AllowPaging = true;
                psd.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                psd.PageSize = AspNetPager1.PageSize;

                this.rptNews.DataSource = psd;
                this.rptNews.DataBind();
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            InitGrid();
        }

        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            InitGrid();
        }
    }
}